package com.sihai.ehr.web.action.exam;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.sys.ExamPageEnum;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.EmployeeService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;

/**
 * 员工考核控制器
 * 
 * @author Henry
 * 
 */
@Results({ @Result(name = "exam_list", location = "exam_main.jsp"),
		@Result(name = "exam_score", location = "exam_score_main.jsp") })
public class ExamFormAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3756092472162318120L;

	Log log = LogFactory.getLog(ExamFormAction.class);

	/**
	 * 组织结构服务
	 */
	@BeanInject
	private OrganizationService organizationService;

	@BeanInject
	private EmployeeService employeeService;

	// *******************************
	// ********* 页面属性 *************
	// *******************************

	/**
	 * 组织架构Bean
	 */
	private Organization organization;
	/**
	 * 组织架构列表
	 */
	private List<Organization> organizations;

	/** 页面编号 */
	private String p;

	/** 功能模块 */
	private String module;

	private String type;
	private String periodDesc;
	private Long workId;
	private String organizationIds;

	@Action("/exam/exam_list")
	public String execute() throws Exception {
		try {
			organizations = organizationService.findChildren(null, true, null,
					null);
			if (organizations != null && organizations.size() > 0) {
				if(getCurrentUserId() > 0){
	            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
	            	Iterator<Organization> it = organizations.iterator();
	            	while(it.hasNext()){
	            		Organization o = it.next();
	            		if(o.getId() != topOrgId){
	            			it.remove();
	            		}
	            	}
	            }
				organizationId = organizations.get(0).getId();
			}
			// 根据传值PageNo获取页面标题及页面内容地址
			listTitle = ExamPageEnum.valueOf(p).getListTitle();
			listAction = ExamPageEnum.valueOf(p).getUrl();
			module = ExamPageEnum.valueOf(p).getBundle();
			showChildOrg = ExamPageEnum.valueOf(p).isRequestShowRelation();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("【考核列表展示异常】【错误原因：" + e.getMessage() + "】【页面ID:" + p
						+ "】");
			}
		}
		return "exam_list";
	}

	@Action("/exam/exam_score")
	public String handleScorePage() throws Exception {

		try {
			organizations = organizationService.findChildren(null, true, null,
					null);
			if (organizations != null && organizations.size() > 0) {
				if(getCurrentUserId() > 0){
	            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
	            	Iterator<Organization> it = organizations.iterator();
	            	while(it.hasNext()){
	            		Organization o = it.next();
	            		if(o.getId() != topOrgId){
	            			it.remove();
	            		}
	            	}
	            }
				organizationId = organizations.get(0).getId();
			}
			// 根据传值PageNo获取页面标题及页面内容地址
			if (ExamPageEnum.valueOf(p) != null) {
				listTitle = ExamPageEnum.valueOf(p).getListTitle();
				listAction = ExamPageEnum.valueOf(p).getUrl();
				module = ExamPageEnum.valueOf(p).getBundle();
				showChildOrg = ExamPageEnum.valueOf(p).isRequestShowRelation();
			}

			if (workId != null && workId > 0) {
				organizationId = employeeService.getEmployeeinfoWork(workId)
						.getOrganization().getId();
				organizationIds = combinationOrganization(organizationId);
				System.out.println(organizationIds);
			}
			
			//fr by henry @{2013-06-18
			try {
				if(getParameter("organizationId") != null){
					organizationId = Long.parseLong(getParameter("organizationId"));
					organizationIds = combinationOrganization(organizationId);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			// fr by henry @}

		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("【考核列表展示异常】【错误原因：" + e.getMessage() + "】【页面ID:" + p
						+ "】");
			}
		}
		return "exam_score";
	}

	public String combinationOrganization(Long organizationId) {
		Organization vOrganization = organizationService.get(organizationId);
		if (vOrganization.getParent() != null) {
			return combinationOrganization(vOrganization.getParent().getId())
					+ "," + vOrganization.getId();
		} else {
			return vOrganization.getId().toString();
		}
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public String getP() {
		return p;
	}

	public void setP(String p) {
		this.p = p;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPeriodDesc() {
		return periodDesc;
	}

	public void setPeriodDesc(String periodDesc) {
		this.periodDesc = periodDesc;
	}

	public Long getWorkId() {
		return workId;
	}

	public void setWorkId(Long workId) {
		this.workId = workId;
	}

	public void setOrganizationIds(String organizationIds) {
		this.organizationIds = organizationIds;
	}

	public String getOrganizationIds() {
		return organizationIds;
	}
}
